<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\base\common;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/13
 * Time: 9:49
 */
class Info extends Controller{
    /*
     * 公告
     * 数据列表
     */
    public function ListItem(){
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        _Db::Validates($request_data);
        $where = '1=1';
        $array_data_range = explode('|', $get_keyword->text);
        if(!empty($get_keyword->text)){
            if (is_array($array_data_range) && $array_data_range[0] !== '' && $array_data_range[1] !== '') {
                $where .= " and a.oper_date between '" . $array_data_range[0] . " 00:00:00'" . " and '" . $array_data_range[1] . " 23:59:59'";
            }
        }
        $param_page = $get_page = input('Page', 1);
        $param_pagesize = 10;
        $result = Db::table('info_manage')
            ->field(
                [
                    'a.information_id'        => 'Id',	        //信息id
                    'a.information_title'     => 'Title',	    //信息标题
                    'a.information_status'    => 'Status',	    //信息状态
                    'a.oper_date'             => 'OperDate',	//操作日期
                    'a.information_type'      => 'InfoType',	//信息类型
                    'd.nick_name'             => 'OperName',    //操作人
                ]
            )
            ->alias('a')
            ->join('staff d','a.oper_id = d.staff_id','LEFT')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->order('a.oper_date desc')
            ->select();
        $data=array();
        foreach ( $result as $key => $value){
            if ($result[$key]['Status'] == 1){
                $result[$key]['Status'] = false;
            }elseif ($result[$key]['Status'] == 2){
                $result[$key]['Status'] = true;
                $result[$key]['Status'] = true;
            };
            $result[$key]['InfoStatus'] = $value['Status'];
            $result[$key]['Order'] = $key + 1;
        };
        $int_total_count = count(Db::table('info_manage')
            ->alias('a')
            ->join('staff d','a.oper_id = d.staff_id','LEFT')
            ->where($where)
            ->order('a.oper_date desc')
            ->select());
        $data['tableData']=$result;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $itemid = new RequestEntity('ItemId', '信息Id', true, 0)
        ];
        _Db::Validates($request_data);
        $param_where = [
            'a.information_id' => $itemid->text,
        ];
        $result = Db::table('info_manage')
            ->field(
                [
                    'a.information_id'        => 'Id',	        //信息id
                    'a.information_title'     => 'Title',	    //信息标题
                    'a.information_content'   => 'Content',	    //信息文字
                    'a.information_status'    => 'Status',	    //信息状态
                    'a.information_type'      => 'InfoType',	//信息类型
                    'a.oper_date'             => 'OperDate',	//操作日期
                    'd.nick_name'             => 'OperName',    //操作人
                ]
            )
            ->alias('a')
            ->join('staff d','a.oper_id = d.staff_id','LEFT')
            ->where($param_where)
            ->find();
        $result['InfoStatus'] = $result['Status'];
        return json_encode($result,true);
    }
    /*
     * 修改数据
     */
    public function ModifyItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', '信息Id', true, 30),
            $post_title = new RequestEntity('Title', '信息标题', true, 30),
            $post_content = new RequestEntity('Content', '信息内容', true, 30),
            $post_type = new RequestEntity('InfoType', '信息类型', true, 50),
            $post_status = new RequestEntity('InfoStatus','信息状态', false, 10),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //验证表单内容
        _Db::Validates($request_data);
        $res = array(
            'information_title'     => $post_title->text,	        //信息标题
            'information_content'   => $post_content->text,         //信息文字
            'information_type'      => $post_type->text,	        //信息类型
            'information_status'    => $post_status->text,	        //信息状态
            'oper_id'               => $post_creatid->text,	        //操作人
            'oper_date'             => date('Y-m-d H:i:s',time()),	//操作日期
        );
        $param_where = [
            'information_id' => $post_itemid->text,
        ];
        $result = Db::table('info_manage')
            ->where($param_where)
            ->update($res);
        if ($result){
            _Db::log($post_creatid->text,3,'信息修改');
            $this->Item($param_where);
        }
    }
    /*
     * 保存数据
     */
    public function AddItem(){
        //获取请求参数
        $request_data = [
            $post_title = new RequestEntity('Title', '信息标题', true, 30),
            $post_content = new RequestEntity('Content', '信息内容', true, 200),
            $post_type = new RequestEntity('InfoType', '信息类型', true, 50),
            $post_status = new RequestEntity('InfoStatus', '信息状态', true, 50),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //验证表单内容
        _Db::Validates($request_data);
        $param_field = [
            'information_title'     => $post_title->text,	        //信息标题
            'information_content'   => $post_content->text,	        //信息文字
            'information_type'      => $post_type->text,	        //信息类型
            'information_status'    => $post_status->text,	        //信息状态(待发布)
            'oper_id'               => $post_creatid->text,	        //操作人
            'oper_date'             => date('Y-m-d H:i:s',time()),	//操作日期
        ];
        $res = Db::table('info_manage')->insert($param_field);
        if ($res){
            _Db::log($post_creatid->text,1,'信息添加');
            $this->ListItem();
        }
    }
    /*
     * 删除表数据
     */
    public function RemoveItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30)
        ];
        //表单内容验证
        _Db::Validates($request_data);
        $param_where = [
            'information_id' => $post_itemid->text,
        ];
        $result = Db::table('info_manage')
            ->where($param_where)
            ->delete();
        if ($result){
            _Db::log($post_creatid->text,2,'信息删除');
            $this->ListItem();
        }

    }
    //是否投放
    public function SwitchModity(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 10),
            $post_value = new RequestEntity('Value', '值', true, 10),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //表单内容验证
        _Db::Validates($request_data);
        $param_field = '';
        if ($post_value->text == 1){
            $type = 6;
            $content = '信息撤销';
            $param_field = [
                'information_status'    => 1,
                'oper_id'               => $post_creatid->text,
                'oper_date' => date("Y-m-d H:i:s",time()),
            ];
        }elseif ($post_value->text == 2){
            $type = 5;
            $content = '信息发布';
            $param_field = [
                'information_status'    => 2,
                'oper_date' => date("Y-m-d H:i:s",time()),
            ];
        }
        //设置条件
        $param_where = [
            'information_id' => $post_itemid->text,
        ];
        $result = Db::table('info_manage')
            ->where($param_where)
            ->update($param_field);
        if ($result){
            _Db::log($post_creatid->text,$type,$content);
            $this->Item($param_where);
        }
    }
    public function Item($param_where){
        $result = Db::table('info_manage')
            ->field(
                [
                    'a.information_id'        => 'Id',	        //信息id
                    'a.information_title'     => 'Title',	    //信息标题
                    'a.information_content'   => 'Content',	    //信息文字
                    'a.information_status'    => 'Status',	    //信息状态
                    'a.information_type'      => 'InfoType',	//信息类型
                    'a.oper_date'             => 'OperDate',	//操作日期
                    'd.nick_name'             => 'OperName',    //操作人
                ]
            )
            ->alias('a')
            ->join('staff d','a.oper_id = d.staff_id','LEFT')
            ->where($param_where)
            ->find();
        $result['InfoStatus'] = $result['Status'];
        if ($result['Status'] == 1){
            $result['Status'] = false;
        }elseif ($result['Status'] == 2){
            $result['Status'] = true;
        };
        _Db::ResponseJson($result);
    }
}